package test6;

import java.util.Arrays;

/**
 * @title:
 * @description:
 * @Author 易俊杰
 * @data: 2022/8/5 14:52
 * @version: V1.0
 */
public class MergeTwoArray {

    static int[] temp;

    public static void main(String[] args) {
        int[] arr1 = {1, 3, 5, 7, 9};
        int[] arr2 = {2, 3, 6, 8, 10, 11, 12};
        int[] nums = {1, 3, 5, 7, 9, 2, 3, 6, 8, 10, 11, 12};
        temp = new int[nums.length];
        merge(nums, 0, arr1.length - 1, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    public static void merge(int[] nums, int lo, int mid, int hi) {
        for (int i = lo; i <= hi; i++) {
            temp[i] = nums[i];
        }
        int i = lo, j = mid + 1;//两个数组开头
        for (int p = lo; p <= hi; p++) {
            if (i == mid + 1) {
                nums[p] = temp[j++];
            } else if (j == hi + 1) {
                nums[p] = temp[i++];
            } else if (temp[i] > temp[j]) {
                nums[p] = temp[j++];
            } else {
                nums[p] = temp[i++];
            }
        }
    }

}
